package com.zk.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zk.erp.entity.Functions;
import com.zk.erp.entity.JsonResult;
import com.zk.erp.entity.RoleFun;
import com.zk.erp.service.IFunctionsService;
import com.zk.erp.service.IRoleFunService;
import com.zk.erp.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-04-12
 */
@RestController
public class RoleFunController {
    @Autowired
    private IRoleFunService roleFunService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IFunctionsService functionsService;

    @RequestMapping(value = "/role-functions",method = RequestMethod.POST)
    public JsonResult addRoleFun(@RequestBody RoleFun roleFun) {
        //查询是否为1级权限
        Functions function = functionsService.getById(roleFun.getFunId());
        //是一级权限
        if (function.getPid().equals("1")) {
            //添加一级权限
            roleFunService.save(roleFun);
            //查询一级权限下的所有子权限
            List<Functions> list = functionsService.list(new QueryWrapper<Functions>().lambda().eq(Functions::getPid, function.getId()));
            //遍历新增所有子权限
            list.forEach(functions -> {
                        if (roleFunService.getOne(new QueryWrapper<RoleFun>().lambda().eq(RoleFun::getRoleId,roleFun.getRoleId()).eq(RoleFun::getFunId,functions.getId()))==null){
                            RoleFun roleFun1 = new RoleFun();
                            roleFun1.setRoleId(roleFun.getRoleId());
                            roleFun1.setFunId(functions.getId());
                            roleFunService.save(roleFun1);
                        }
                    }
            );
        } else {
            //不是一级权限

            //查询子权限
            Functions byId = functionsService.getById(roleFun.getFunId());
            //根据子权限的Pid查询所有该父权限的所有子权限
            List<Functions> functions = functionsService.list(new QueryWrapper<Functions>().lambda().eq(Functions::getPid, byId.getPid()));
            for (Functions function1 : functions) {
                //查询roleFun里是否有该子权限
                RoleFun one = roleFunService.getOne(new QueryWrapper<RoleFun>().lambda().eq(RoleFun::getFunId, function1.getId()).eq(RoleFun::getId,roleFun.getRoleId()));
                if (one == null) {
                    return new JsonResult(1, null);
                }
            }
            //查询roleFun中是否有该父权限
            if (roleFunService.getOne(new QueryWrapper<RoleFun>().lambda().eq(RoleFun::getRoleId,roleFun.getRoleId()).eq(RoleFun::getFunId,byId.getPid()))!=null){
                //如果都有，则添加其父权限
                Functions one = functionsService.getOne(new QueryWrapper<Functions>().lambda().eq(Functions::getId, byId.getPid()));
                RoleFun roleFun1 = new RoleFun();
                roleFun1.setRoleId(roleFun.getRoleId());
                roleFun1.setFunId(one.getId());
                roleFunService.save(roleFun1);
            }

        }
        return new JsonResult(1, null);

    }

    @RequestMapping(value = "/role-functions/{roleId}/{funId}",method = RequestMethod.DELETE)
    public JsonResult deleteRoleFun(@PathVariable("roleId")String roleId,@PathVariable("funId")String funId){
        //查询是否为1级权限
        Functions function = functionsService.getById(funId);
        //是一级权限
        if (function.getPid().equals("1")){
            //查询一级权限下的所有子权限
            List<Functions> list = functionsService.list(new QueryWrapper<Functions>().lambda().eq(Functions::getPid, function.getId()));
            //遍历删除所有子权限
            list.forEach(functions -> {
                        roleFunService.remove(new QueryWrapper<RoleFun>().lambda().eq(RoleFun::getFunId,functions.getId()).eq(RoleFun::getRoleId,roleId));
                    }
            );
            //删除一级权限
            roleFunService.remove(new QueryWrapper<RoleFun>().lambda().eq(RoleFun::getRoleId,roleId).eq(RoleFun::getFunId,funId));
        }
        //不是一级权限
        else {
            roleFunService.remove(new QueryWrapper<RoleFun>().lambda().eq(RoleFun::getRoleId,roleId).eq(RoleFun::getFunId,funId));
        }
        return new JsonResult(1,null);
    }
}
